package com.base.timer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.controller.api.RedisCacheManager;
import com.base.controller.utils.CZB.CZBTokenUtils;
import com.beust.jcommander.internal.Lists;
import com.beust.jcommander.internal.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/getALlByRedis")
public class CZB_GetAllToRedis {
    @Autowired
    private RedisCacheManager redisCacheManager;

    @RequestMapping("/getSearch")
    public @ResponseBody
    Boolean insertGasToRedis1() {
        System.out.println("开始更新redis");
        Long date = new Date().getTime();
        String token = CZBTokenUtils.getNowToken("18055603791");
        String result = CZBTokenUtils.getAllGasInfo(date, "18055603791", token);
        JSONObject r1 = JSONObject.parseObject(result);
        JSONArray jsonArray = (JSONArray) r1.get("result");
        Map<String, Object> gasListMap = Maps.newHashMap();
        jsonArray.forEach(item1 -> {
            JSONObject gas = JSONObject.parseObject(item1.toString());
            String gasid = gas.getString("gasId");
            String gasName = gas.getString("gasName");
            String provinceCode = gas.getString("provinceCode");
            BigDecimal gasAddressLongitude = new BigDecimal(gas.getString("gasAddressLongitude"));
            BigDecimal gasAddressLatitude = new BigDecimal(gas.getString("gasAddressLatitude"));
            String gasLogoSmall = gas.getString("gasLogoSmall");
            String gasLogoBig = gas.getString("gasLogoBig");
            String gasAddress = gas.getString("gasAddress");
            String provinceName = gas.getString("provinceName");
            String cityName = gas.getString("cityName");
            String countyName = gas.getString("countyName");
            String address = provinceName + " " + cityName + " " + countyName;
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasAddress", gasAddress == null ? "" : gasAddress);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasid", gasid == null ? "" : gasid);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasName", gasName == null ? "" : gasName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gaslogosmall", gasLogoSmall == null ? "" : gasLogoSmall);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gaslogobig", gasLogoBig == null ? "" : gasLogoSmall);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "address", address == null ? "" : address);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "provinceName", provinceName == null ? "" : provinceName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "cityName", cityName == null ? "" : cityName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "countyName", countyName == null ? "" : countyName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "longitude", gasAddressLongitude.toString());
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "latitude", gasAddressLatitude.toString());
            JSONArray oilpriceList = (JSONArray) gas.get("oilPriceList");
            List<String> list1 = Lists.newArrayList();
            List<String> list2 = Lists.newArrayList();
            for (Object oilPrice1 : oilpriceList) {
                JSONObject oilPrice = JSONObject.parseObject(oilPrice1.toString());

                String oilNo = oilPrice.getString("oilNo");
                String oilName = oilPrice.getString("oilName");
                String oilType = oilPrice.getString("oilType");
                String priceYfq = oilPrice.getString("priceYfq");
                String priceGun = oilPrice.getString("priceGun");
                String priceOfficial = oilPrice.getString("priceOfficial");
                list1.add(oilNo);
                list2.add(oilName);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "gasId", gasid);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "oilType", oilType);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceYfqSearch", priceYfq);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceGunSearch", priceGun);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceOfficialSearch", priceOfficial);
            }
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "oilNo", list1.toString());
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "oilName", list2.toString());
            if (gasListMap.get(provinceCode) != null) {
                String str = gasListMap.get(provinceCode).toString();
                gasListMap.put(provinceCode, str + "," + gasid);
            } else {
                gasListMap.put(provinceCode, gasid);
            }

        });
        Iterator iter = gasListMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            String[] gasListString = val.split(","); //将油站ID进行分割，拿到所有的油站信息
            List<String> valList = Lists.newArrayList(gasListString);
            Boolean flag = redisCacheManager.lSet("gasListProvince:" + key, val);
            System.out.println(flag);
        }


        System.out.println("循环完成" + new Date());
        return true;
    }

    /**
     * @Date: 2019/11/22 10:14
     * @Description: 由于搜索请求速度过慢，用户体验太差，将数据定时存储到redis中！！！
     */
    @Scheduled(cron = "0 0 6,9,12,14,16,19 * * ?")
    public void insertGasToRedis() {
        System.out.println("开始更新redis");
        Long date = new Date().getTime();
        String token = CZBTokenUtils.getNowToken("18055603791");
        String result = CZBTokenUtils.getAllGasInfo(date, "18055603791", token);
        JSONObject r1 = JSONObject.parseObject(result);
        JSONArray jsonArray = (JSONArray) r1.get("result");
        Map<String, Object> gasListMap = Maps.newHashMap();
        jsonArray.forEach(item1 -> {
            JSONObject gas = JSONObject.parseObject(item1.toString());
            String gasid = gas.getString("gasId");
            String gasName = gas.getString("gasName");
            String provinceCode = gas.getString("provinceCode");
            BigDecimal gasAddressLongitude = new BigDecimal(gas.getString("gasAddressLongitude"));
            BigDecimal gasAddressLatitude = new BigDecimal(gas.getString("gasAddressLatitude"));
            String gasLogoSmall = gas.getString("gasLogoSmall");
            String gasLogoBig = gas.getString("gasLogoBig");
            String gasAddress = gas.getString("gasAddress");
            String provinceName = gas.getString("provinceName");
            String cityName = gas.getString("cityName");
            String countyName = gas.getString("countyName");
            String address = provinceName + " " + cityName + " " + countyName;
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasAddress", gasAddress == null ? "" : gasAddress);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasid", gasid == null ? "" : gasid);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gasName", gasName == null ? "" : gasName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gaslogosmall", gasLogoSmall == null ? "" : gasLogoSmall);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "gaslogobig", gasLogoBig == null ? "" : gasLogoSmall);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "address", address == null ? "" : address);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "provinceName", provinceName == null ? "" : provinceName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "cityName", cityName == null ? "" : cityName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "countyName", countyName == null ? "" : countyName);
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "longitude", gasAddressLongitude.toString());
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "latitude", gasAddressLatitude.toString());
            JSONArray oilpriceList = (JSONArray) gas.get("oilPriceList");
            List<String> list1 = Lists.newArrayList();
            List<String> list2 = Lists.newArrayList();
            for (Object oilPrice1 : oilpriceList) {
                JSONObject oilPrice = JSONObject.parseObject(oilPrice1.toString());

                String oilNo = oilPrice.getString("oilNo");
                String oilName = oilPrice.getString("oilName");
                String oilType = oilPrice.getString("oilType");
                String priceYfq = oilPrice.getString("priceYfq");
                String priceGun = oilPrice.getString("priceGun");
                String priceOfficial = oilPrice.getString("priceOfficial");
                list1.add(oilNo);
                list2.add(oilName);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "gasId", gasid);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "oilType", oilType);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceYfqSearch", priceYfq);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceGunSearch", priceGun);
                redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid + ":" + oilNo, "priceOfficialSearch", priceOfficial);
            }
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "oilNo", list1.toString());
            redisCacheManager.hset("GasList:" + provinceCode + "gasID:" + gasid, "oilName", list2.toString());
            if (gasListMap.get(provinceCode) != null) {
                String str = gasListMap.get(provinceCode).toString();
                gasListMap.put(provinceCode, str + "," + gasid);
            } else {
                gasListMap.put(provinceCode, gasid);
            }

        });
        Iterator iter = gasListMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            String val = entry.getValue().toString();
            String[] gasListString = val.split(","); //将油站ID进行分割，拿到所有的油站信息
            List<String> valList = Lists.newArrayList(gasListString);
            Boolean flag = redisCacheManager.lSet("gasListProvince:" + key, val);
            System.out.println(flag);
        }
        System.out.println("循环完成" + new Date());
    }
}
